package com.turbo.cloud.contract.web.logic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.turbo.cloud.contract.constants.ContractConstants;
import com.turbo.cloud.contract.web.entity.CtContractScene;
import com.turbo.cloud.contract.web.model.scene.CtContractSceneQO;
import com.turbo.cloud.contract.web.model.scene.CtContractSceneDO;
import com.turbo.cloud.contract.web.model.scene.CtContractSceneVO;
import com.turbo.cloud.contract.web.service.CtContractSceneService;
import com.turbo.cloud.core.base.PageQuery;
import com.turbo.cloud.core.exception.BusinessException;
import com.turbo.cloud.core.utils.PageQueryUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 合同场景 逻辑处理类
 * </p>
 *
 * @author zhangluning
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class SceneLogic {
    final CtContractSceneService ctContractSceneService;
    /**
     * 查询表单列表
     *
     * @param pageQuery 查询表单条件
     * @return Page
     */
    public PageQuery<CtContractSceneVO> queryContractScenePage(PageQuery<CtContractSceneQO> pageQuery) {
        CtContractSceneQO ctContractSceneQO = PageQueryUtil.getPageParam(pageQuery, CtContractSceneQO::new);

        /*
         * 模糊查询条件
         */
        LambdaQueryWrapper<CtContractScene> contractSceneLQW = new LambdaQueryWrapper<>();

        contractSceneLQW.like(StrUtil.isNotBlank(ctContractSceneQO.getSceneCodeByLike()),
                CtContractScene::getSceneCode,
                ctContractSceneQO.getSceneCodeByLike());
        contractSceneLQW.like(StrUtil.isNotBlank(ctContractSceneQO.getSceneNameByLike()),
                CtContractScene::getSceneName,
                ctContractSceneQO.getSceneNameByLike());

        contractSceneLQW.orderByAsc(CtContractScene::getSort);

        Page<CtContractScene> contractScenePage = ctContractSceneService.page(pageQuery.toPage(), contractSceneLQW);

        PageQuery<CtContractSceneVO> contractSceneVOPage = PageQueryUtil.toPageQuery(contractScenePage, CtContractSceneVO::new);

        return contractSceneVOPage;
    }


    /**
     * 新增合同模板
     *
     * @param ctContractSceneDO 实体
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean createContractScene(CtContractSceneDO ctContractSceneDO) {
        CtContractScene ctContractScene = new CtContractScene();
        BeanUtils.copyProperties(ctContractScene, ctContractScene);

        boolean roleSaveResult = ctContractSceneService.save(ctContractScene);
        if (!roleSaveResult) {
            throw new BusinessException("新增合同模板失败");
        }
        return true;
    }

    /**
     * 修改合同模板
     *
     * @param ctContractSceneDO 实体
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyContractScene(CtContractSceneDO ctContractSceneDO) {
        CtContractScene ctContractScene = new CtContractScene();
        BeanUtils.copyProperties(ctContractScene, ctContractScene);

        boolean result = ctContractSceneService.updateById(ctContractScene);
        if (!result) {
            throw new BusinessException("修改合同模板失败");
        }

        return true;
    }

    /**
     * 开启、关闭合同模板有效状态
     *
     * @param id 合同模板ID
     * @return
     */
    public Boolean enabledContractSceneStateById(String id) {
        CtContractScene ctContractScene = ctContractSceneService.getById(id);
        if (Objects.isNull(ctContractScene)) {
            throw new BusinessException("合同模板不存在");
        }

        if (ContractConstants.DICT_STATE_ID_YES.equals(ctContractScene.getStateId())) {
            ctContractScene.setStateId(0);
        } else {
            ctContractScene.setStateId(1);
        }

        boolean result = ctContractSceneService.updateById(ctContractScene);
        if (!result) {
            throw new BusinessException("修改合同模板状态失败");
        }
        return true;
    }

    /**
     * 删除合同模板
     *
     * @param id 合同模板ID
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean removeContractSceneById(String id) {
        boolean result = ctContractSceneService.removeById(id);
        if (!result) {
            throw new BusinessException("删除合同模板失败");
        }
        return true;
    }
}
